package com.nosweattechnet.mapmyway;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.nosweattechnet.locationsearch.GivenLocationSearchActivity;
import com.nosweattechnet.locationsearch.VariableLocationSearchActivity;

public class MainActivity extends MapActivity {
	
	public final static String EXTRA_MESSAGE = "com.nosweattechnet.mapmyway.MESSAGE";
	private String userid;
	private Drawable pin;
	private MapItemizedOverlay itemizedoverlay;
	private List<Overlay> mapOverlays;
	private MapView mapView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    
	    mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    // creates a unique id for this installation of the app
	    // and stores it on the phone
	    userid = Storage.GenerateID(this);
	    
	    // creates list to store overlay items
	    mapOverlays = mapView.getOverlays();
	    
	    pin = this.getResources().getDrawable(R.drawable.google_pin_small);
	    itemizedoverlay = new MapItemizedOverlay(pin, this,userid);
	    mapOverlays.add(itemizedoverlay);
	}
	
	@Override
    public void onResume(){
    	super.onResume();
    	showPlaces();
    }
	
	//Adds all of the places to the main map then forces it to refresh
	private void showPlaces(){
		DisplayPlaces.addToItemizedOverlay(itemizedoverlay);
		mapView.invalidate();
	}
	
	//Performs a location search where the user selects the radius
	private void radiusSearch(){
		Intent intent=new Intent(this,VariableLocationSearchActivity.class);
		startActivity(intent);
	}
	
	//Performs a location search with a specified radius
	private void radiusSearch(float r){
		Intent intent=new Intent(this,GivenLocationSearchActivity.class);
		intent.putExtra(getString(R.string.radius_intent), r);
		startActivity(intent);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        	//Keyword search
            case R.id.menu_search:
                onSearchRequested();
                return true;
            //View the favorites list
            case R.id.placelist:
            	Intent intent=new Intent(this,PlaceListActivity.class);
            	intent.putExtra(getString(R.string.user_id_intent), userid);
            	startActivity(intent);
            	return true;
            //Removes all markers on the map and redisplays the map
            case R.id.removeAllPlaceMarkers:
            	DisplayPlaces.clear();
            	showPlaces();
            	return true;
            //Allows the user to specify the radius of a location search
            case R.id.variableSearch:
            	radiusSearch();
            	return true;
            //Performs a location search with a set radius
            case R.id.tenMileSearch:
            	radiusSearch((float)10);
            	return true;
            case R.id.twentyfiveMileSearch:
            	radiusSearch((float)25);
            	return true;
            case R.id.onehunderedMileSearch:
            	radiusSearch((float)100);
            	return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    //Doesn't display the a routeon the mpa ever
    protected boolean isRouteDisplayed() {
        return false;
    }
}
